<template>
    <div class="main">
        <van-search placeholder="请输入搜索关键词" v-model="q" @search="onSearch"/>
        <van-swipe :autoplay="3000" indicator-color="white">
            <van-swipe-item v-for="(item,index) in slide" :key="index" @click="onShowPopup(item)">
                <img v-lazy="item.pic_url+'_400x400'"/>
            </van-swipe-item>
        </van-swipe>
        <nav class="daohang">
            <router-link to="/list" class="item">
                <span class="iconfont tupian">&#xe63e;</span>
                <span class="wenben">好券直播</span>
            </router-link>
            <router-link to="/da_e_list" class="item">
                <span class="iconfont tupian">&#xe610;</span>
                <span class="wenben">大额券</span>
            </router-link>
            <router-link to="/pin_pai_list" class="item">
                <span class="iconfont tupian">&#xe7b8;</span>
                <span class="wenben">品牌券</span>
            </router-link>
            <router-link to="/te_hui_list" class="item">
                <span class="iconfont tupian">&#xe602;</span>
                <span class="wenben">特惠商品</span>
            </router-link>
            <router-link to="/liu_xing_list" class="item">
                <span class="iconfont tupian">&#xe601;</span>
                <span class="wenben">潮流趋势</span>
            </router-link>
        </nav>
        <van-list v-model="loading"
                  :finished="finished"
                  :error.sync="error"
                  finished-text="我是有底线的!"
                  error-text="请求失败，点击重新加载"
                  @load="onLoad">
            <tk-goods v-for="item in list" :key="item.id" :data="item"></tk-goods>
        </van-list>
        <van-popup v-model="showToken">
            <div class="taokouling">
                <h3 class="biaoti">淘口令</h3>
                <input type="text" :value="slideItem.token" ref="tkl" class="text">
                <button class="anniu" @click="copy('tkl')">一键复制</button>
                <p class="beizhu">（若复制失败，请长按文字手动复制）</p>
                <p class="help">复制"淘口令"后打开【手机淘宝】即可领取优惠券购买</p>
            </div>
        </van-popup>
    </div>
</template>
<script>
    import {Search, List, Swipe, SwipeItem,Popup,Toast} from 'vant';
    import Goods from '../components/Goods.vue';

    export default {
        data() {
            return {
                q: "",
                page: 1,
                rows: 20,
                list: [],
                slide: [],
                slideItem:{},
                showToken:false,
                finished: false,
                loading: false,
                error: false
            }
        },
        components: {
            [Search.name]: Search,
            [List.name]: List,
            [Swipe.name]: Swipe,
            [SwipeItem.name]: SwipeItem,
            [Goods.name]: Goods,
            [Popup.name]: Popup,
            [Toast.name]:Toast
        },
        methods: {
            onShowPopup(item) {
                this.slideItem = item;
                this.showToken = true;
            },
            onSearch() {
                this.$router.push({
                    path: "/search",
                    query: {
                        q: this.q
                    }
                });
            },
            copy(el) {
                this.$refs[el].select();
                if (document.execCommand('Copy')) {
                    Toast('复制成功');
                } else {
                    Toast('当前浏览器不支持一键复制功能，请长按内容手动复制');
                }
            },
            onLoad() {
                this.request(this.$store.api.list + '/4092', {
                    page: this.page++,
                    rows: this.rows
                });
            },
            async getSlide() {
                const result = await this.$http.getStore(this.$store.api.slide);
                this.slide = result['data'];
            },
            async request(url, params) {
                this.loading = true;
                try {
                    const result = await this.$http.getStore(url, params);
                    this.list.push(...result['data']);
                } catch (e) {
                    this.error = true;
                }
                this.loading = false;
            }
        },
        mounted() {
            this.getSlide();
        }
    }
</script>
<style scoped>
    .van-swipe {
        width: 100%;
        height: calc(100vw * 41 / 100);
    }
    
    .van-list {
        margin: 10px;
    }
    
    .daohang {
        display: flex;
        flex-direction: row;
        justify-content: space-around;
        align-items: center;
        flex-wrap: wrap;
        padding-bottom: 10px;
        background: #fff;
    }
    
    .daohang .item {
        flex-basis: 16%;
        display: flex;
        flex-direction: column;
        justify-content: center;
        text-align: center;
        margin-top: 10px;
    }
    
    .daohang .item .tupian {
        font-size: 32px;
        height: calc(100vw * 0.16);
        line-height: calc(100vw * 0.16);
        border-radius: 10px;
        background: #ff718b;
        text-align: center;
        color: #fff;
    }
    
    .daohang .item .wenben {
        font-size: 12px;
        line-height: 26px;
        height: 26px;
        overflow: hidden;
        color: #555;
    }

    .taokouling{
        display: flex;
        flex-direction: column;
        background: #fff;
        padding: 20px;
        border-radius: 3px;
        box-shadow: 1px 0 4px #ccc;
    }

    .taokouling .biaoti{
        display: block;
        text-align: center;
        margin-bottom: 15px;
        font-size: 16px;
    }

    .taokouling .text {
        border: 1px #ff718b dashed;
        padding: 20px;
        font-size: 16px;
        background: #fff;
        color: #333;
        font-weight: 700;
        text-align: center;
    }
    .taokouling .anniu{
        padding: 10px 20px;
        background: #ff718b;
        color: #fff;
        font-size: 14px;
        border-radius: 3px;
        border: 0;
        margin-top: 10px;
        cursor: pointer;
        line-height: 22px;
    }

    .taokouling .anniu:hover {
        opacity: .8;
        background: #e71a3f;
    }

    .taokouling .beizhu {
        font-size: 14px;
        color: #666;
        text-align: center;
        line-height: 24px;
        margin-top: 10px;
    }

    .taokouling .help {
        margin-top: 10px;
        font-size: 14px;
        color: #333;
        line-height: 22px;
    }
    .van-popup {
        width: 90%;
    }
</style>